package com.farm.controller;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.opjo.Index;
import com.farm.reception.WebIndex;
import com.farm.service.IndexService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 新增，修改，前台传的参数  和 分页条件查询是我们传给前台的参数 是同一个实体类封装 ，接收参数
 */
@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private IndexService indexService;
    /**
     * 分页+条件查询(name)
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result searchSelect(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size){
       //Page<WebIndex> webDiseases = indexService.findPage ( searchMap, page, size );
        return new Result ( true, StatusCode.ACCESSERROR, "查询成功",null);
    }

    /**
     * 更新
     * @param webIndex
     * @return
     */
    @PutMapping("/update")
    public  Result update(@RequestBody WebIndex webIndex){
        indexService.update (webIndex.getIndex ());
        return new Result ( true, StatusCode.ACCESSERROR, "修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public  Result delete(@RequestParam int id){
        indexService.delete ( id );
        return new Result ( true, StatusCode.ACCESSERROR, "删除成功");
    }

    /**
     * 新增
     * @param webIndex
     * @return
     */
    @PostMapping("/add")
    public  Result add(@RequestBody WebIndex webIndex){
        indexService.add ( webIndex.getIndex () );
        return new Result ( true, StatusCode.ACCESSERROR, "新增成功");
    }

    /**
     * 根据Id获取指标
     * 用于feign接口调用
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result<Index> findById(@PathVariable("id") Integer id){
        Index index = indexService.findById ( id );
        return new Result ( true, StatusCode.ACCESSERROR, "查询成功",index);

    }
}
